package dao;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
class IOHandler {
	static List<String> readAsList(File file) {
		List<String> list = new ArrayList<String>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(
				new InputStreamReader(new FileInputStream(file)/*,"utf-8"*/));
			String line = null;
			while((line = reader.readLine()) != null) {
				list.add(line);
			}
		} catch(IOException e) {
			e.printStackTrace();
		} finally {
			if(reader != null)
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return list;
	}
	
	static List<String> _readAsList(InputStream inputstream){
		List<String> list = new ArrayList<String>();
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(
				new InputStreamReader(inputstream/*,"utf-8"*/));
			String line = null;
			while((line = reader.readLine()) != null) {
				list.add(line);
			}
		} catch(IOException e) {
			e.printStackTrace();
		} finally {
			if(reader != null)
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return list;
		
	}
	
	static void serialize2File(Collection<String> list, File file) {
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(
					new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			for(String line : list) { 
				writer.write(line);
				writer.newLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(writer != null) {
				try {
					writer.flush();
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}
	}
	
	static void nextserialize2File(Collection<String> list, File file) {
		BufferedWriter writer = null;
		try {
			writer = new BufferedWriter(
					new OutputStreamWriter(new FileOutputStream(file,true)/*, "UTF-8"*/));
			for(String line : list) { 
				writer.write(line);
				writer.newLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(writer != null) {
				try {
					writer.flush();
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}
	}
	
    static FileInputStream getFileResourceAsStream(String url) {
		FileInputStream is = null;
		try {
			is = new FileInputStream(url);
		} catch (FileNotFoundException e1) {
			is = null;
		}
		return is;
	}
	
    /**
	 * 过滤出目录下以.txt为后缀的文件
	 * @param Folder：目录
	 * @return
	 */
	static File[] getTxtFiles(File folder) {
		return folder.listFiles(new FileFilter() {
			public boolean accept(File file) {
				return file.getName().endsWith(".txt");
			}
			
		});
	}
	
	/**
	 * 过滤出kclusterFolder目录下以.seg为后缀的文件
	 * @param kclusterFolder
	 * @return
	 */
	static File[] getSegFiles(File folder) {
		return folder.listFiles(new FileFilter() {
			public boolean accept(File file) {
				return file.getName().endsWith(".seg");
			}
		});
	}
    
	/**
	 * 文件拷贝
	 * @param sourcefilepath 需要拷贝的文件
	 * @param distfilepath 存放目标文件的文件目录
	 */
	void copy_file(String sourcefilepath,String distfilepath) {
		File sourceFile = new File(sourcefilepath);
		if(!sourceFile.exists())
		{
			System.out.println(sourcefilepath+"文件不存在！");
		}
		if(!sourceFile.isDirectory()){
			List<String> rList = readAsList(sourceFile);
			serialize2File(rList, 
					new File(distfilepath+"/"+sourceFile.getName()));
			return;
		}
		//如果是文件夹
		File distFile = new File(distfilepath+"/"+sourceFile.getName());
			distFile.mkdir();
			for(File file:sourceFile.listFiles())
				copy_file(file.getPath(), distFile.getPath());
		}
}
